package show.me.the.money.util.exp.standard;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.hibernate.SessionFactory;
import org.hibernate.impl.SessionFactoryImpl;
import org.hibernate.impl.SessionImpl;
import org.springframework.web.context.WebApplicationContext;

import show.me.the.money.util.AppContextHolder;
import show.me.the.money.util.context.Context;
import show.me.the.money.util.exp.ExpRunner;
import show.me.the.money.util.exp.ExpSymbols;
import show.me.the.money.util.exp.Expression;


public class CONVERT extends Expression {

	//this class is to resolve several database
	public CONVERT() {
		symbol = ExpSymbols.CONVERT;
	}

	public String run(List lsExp, Context ctx) {
		try {
			StringBuilder sbuilder=new StringBuilder();
			List la1 = (List)lsExp.get(1);
			List la2 = (List)lsExp.get(2);
			String s1=(String)ExpRunner.toString(la1, ctx);
			String s2=(String)ExpRunner.run(la2, ctx);
//			sbuilder.append(symbol).append("(").append(ExpRunner.run(la1, ctx)).append(",");
//			sbuilder.append(ExpRunner.toString(la2, ctx)).append(",").append(format).append(")");
			return ExpUtil.getType(s2).replace("{0}", s1);
		} catch (Exception e) {
			logger.fatal("run JSONException:", e);
		}
		return symbol + "()" ;
	}

	public String toString(List lsExp, Context ctx) {
		try {
			StringBuilder sbuilder=new StringBuilder();
			List la1 = (List)lsExp.get(1);
			List la2 = (List)lsExp.get(2);
			String s1=(String)ExpRunner.toString(la1, ctx);
			String s2=(String)ExpRunner.run(la2, ctx);
			return ExpUtil.getType(s2).replace("{0}", s1);
			
//			String format="121";  //default format is 'YYYY-MM-DD'
//			StringBuilder sbuilder=new StringBuilder();
//			List la1 = (List)lsExp.get(1);
//			List la2 = (List)lsExp.get(2);
//			if(lsExp.size()>3){
//				format =(String)ExpRunner.run((List)lsExp.get(3), ctx);
//			}
//			sbuilder.append(symbol).append("(").append(ExpRunner.run(la1, ctx)).append(",");
//			sbuilder.append(ExpRunner.toString(la2, ctx)).append(",").append(format).append(")");
//			return sbuilder.toString();
		} catch (Exception e) {
			logger.fatal("run JSONException:", e);
		}
		return symbol + "()";
	}
	
}
